package com.tg.web.backend;

import com.github.pagehelper.Page;
import com.tg.model.EbProduct;
import com.tg.model.EbProductCategory;
import com.tg.service.ProductCategoryService;
import com.tg.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

/**
 * @Description: 用一句话描述
 * @Auther: 新梦想*陈超
 * @Date: 2020-10-19 15:51
 */

@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;//低耦合

    @Autowired
    private ProductCategoryService productCategoryService;

    //1.商品的分页查询方法---分页查询的请求
    //分页：${pageContext.request.contextPath}/product/list?page=当前页-1
    @RequestMapping("/list")
    public String list(Model model,
                       @RequestParam(value = "page",defaultValue = "1") int page,
                       @RequestParam(value = "pageSize",defaultValue = "5")  int pageSize){
        System.out.println("receive browser proruct page request!!!");
        //1.调用service层的分页查询商品信息的方法
        List<EbProduct> ebProducts = productService.list(page, pageSize);
        //2.返回model(ebProducts)和视图(jsp)
        model.addAttribute("ebProducts",ebProducts);//key-value  你名字---你

        //2.1.传递当前页到product.jsp
        model.addAttribute("curPage",page);
        //2.2 传递最大页到product.jsp
        model.addAttribute("maxPage",((Page)ebProducts).getPages());


        return "manage/product";
    }
    //2.商品添加的方法---商品添加的请求
    @RequestMapping("/to_save")
    public String toSave(Model model){
        //查询所有大类和小类
        //1.调用商品分类的service层 查询大类和小类
        Map<String, List<EbProductCategory>> categoryMap = productCategoryService.findBigAndSmallCategory();
        //2.通过model传递给jsp
        model.addAttribute("categoryMap",categoryMap);

        return "manage/product-add";
    }
    @RequestMapping("/save")
    public String save(EbProduct ebProduct){
        System.out.println("ebProduct==>"+ebProduct);
        return null;
    }



    //3.删除商品的方法---删除商品的请求
    //4.更新商品的方法---更新商品的请求

}
